package com.lyandwc.lw_bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyandwc.lw_bbs.dao.TopicTagDao;
import com.lyandwc.lw_bbs.entity.Tag;
import com.lyandwc.lw_bbs.entity.TopicTag;
import com.lyandwc.lw_bbs.service.TopicTagService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TopicTagServiceImpl extends ServiceImpl<TopicTagDao, TopicTag> implements TopicTagService {
    @Override
    public List<TopicTag> updateTopicTags(int topicId, List<Tag> tags) {
        this.baseMapper.delete(
                new QueryWrapper<TopicTag>().lambda()
                        .eq(TopicTag::getTopicId, topicId)
        );

        List<TopicTag> topicTags = new ArrayList<>();

        for (Tag tag : tags) {
            TopicTag topicTag = new TopicTag();
            topicTag.setTopicId(topicId);
            topicTag.setTagId(tag.getId());
            topicTags.add(topicTag);
        }

        this.saveBatch(topicTags);

        return topicTags;
    }
}
